<template>
  <div class="wrapper">
    <div id="login">
      <div class="title">
        <p>请先登录</p>
      </div>
      <div class="label-input">
        <input placeholder="账号" id="username" type="text" v-model="username" />
      </div>
        <div class="label-input"  @keydown="keydown">
          <input placeholder="密码" id="password" type="password" v-model="password" />
        </div>
      <button @click="login" :class="{ forbidden: landing }">
        <p v-show="!landing">登陆</p>
        <p v-show="landing"><LoadingSvg /></p>
      </button>
    </div>
  </div>
</template>

<script>
import login from '@/request/login.js'
import LoadingSvg from '@/components/LoadingSvg.vue'

export default {
  data() {
    return {
      username: '',
      password: '',
      landing: false,
    }
  },
  methods: {
    async login() {
      if (this.landing) return
      this.landing = true
      try {
        const loginStatus = await login({ username: this.username, password: this.password })
        if (loginStatus) {
          console.log('debugger: ', this.$router)
          this.$router.replace('/home')
        } else {
          this.$msg('登陆失败，如忘记密码，请联系开发人员', 'error')
        }
      } catch (error) {
        console.error(error.message)
        this.$msg('error: 出错了', 'error')
      }
      this.landing = false
    },
    keydown(e) {
      if (e.code === 'Enter' && !!this.username.trim() && !!this.password) {
        this.login()
      }
    }
  },
  components: { LoadingSvg },
}
</script>

<style lang="scss">
.wrapper {
  width: 100%;
  height: 100vh;
  margin: 0;
  display: grid;
  place-content: center;
}

#login {
  width: 300px;
  height: 200px;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0 0 8px 0 #eee;
  border-radius: 4px;

  .title {
    width: 100%;
    height: 40px;
    border-bottom: 1px solid #ddd;
    p {
      width: 100%;
      text-align: center;
      font-weight: bold;
    }
  }
  .label-input {
    width: 100%;
    height: 40px;
    padding: 5px 0;
    border-bottom: 1px solid #ddd;
    input {
      width: 100%;
      height: 30px;
      padding: 0;
      line-height: 30px;
      outline: none;
      border: none;
      text-align: center;
      font-size: 14px;
    }
  }

  button {
    width: 100px;
    height: 34px;
    margin: 20px auto;
    background: skyblue;
    color: #fff;
    border: none;
    outline: none;
    box-shadow: 0 0 4px 0 skyblue;
    cursor: pointer;
    display: grid;
    place-content: center;
    &:hover {
      filter: brightness(1.05);
    }
    &:active {
      filter: brightness(0.9);
    }
  }
}
</style>
